package com.cskaoyan.service.promotion;

import com.cskaoyan.bean.promotion.Topic;
import com.cskaoyan.bean.promotion.TopicExample;
import com.cskaoyan.mapper.TopicMapper;
import com.cskaoyan.utils.DataUtils;
import com.cskaoyan.vo.promotion.TopicListVo;
import com.cskaoyan.vo.promotion.WxTopicDetailVo;
import com.cskaoyan.vo.promotion.WxTopicListTopicVo;
import com.cskaoyan.vo.promotion.WxTopicListVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @description:
 * @autyor:caicaicai
 */
@Service
public class TopicServiceImpl implements TopicService {

    @Autowired
    TopicMapper topicMapper;

    @Override
    public TopicListVo list(Integer page, Integer limit, String sort, String order, String title, String subtitle) {
        PageHelper.startPage(page, limit);
        TopicExample topicExample = new TopicExample();
        topicExample.setOrderByClause(sort + " " + order);
        //分页 排序 条件查询（2个条件都是模糊查询） 非逻辑删除deleted=false
        //where deleted=false and title like "%title%" and subtitle like "%subTitle%"
        TopicExample.Criteria criteria1 = topicExample.createCriteria();//sql语句的拼接${}
        criteria1.andDeletedEqualTo(false);//逻辑删除为false
        if(title != null && !"".equals(title)){
            criteria1.andTitleLike("%" + title + "%");
        }
        if(subtitle != null && !"".equals(subtitle)){
            criteria1.andSubtitleLike("%" + subtitle + "%");
        }

        List<Topic> topics = topicMapper.selectByExample(topicExample);
        PageInfo<Topic> topicPageInfo = new PageInfo<>(topics);
        long total1 = topicPageInfo.getTotal();

        TopicListVo topicListVo = new TopicListVo();
        topicListVo.setItems(topics);
        topicListVo.setTotal(total1);
        return topicListVo;
    }

    @Override
    public int update(Topic topic) {
        int affectedRows = topicMapper.updateByPrimaryKeySelective(topic);
        if(affectedRows == 1){
            return 200;
        }
        return 404;
    }

    @Override
    public int delete(Topic topic) {
        //逻辑删除，即修改属性deleted=true
        topic.setDeleted(true);
        int affectedRows =topicMapper.updateByPrimaryKeySelective(topic);
        if(affectedRows == 1){
            return 200;
        }
        return 404;
    }

    @Override
    public Topic create(Topic topic) {
        Date nowDate = DataUtils.getCurrentTime();
        topic.setAddTime(nowDate);
        topic.setUpdateTime(nowDate);

        //插入数据后，主键被塞回输入映射的对象中
        topicMapper.insertSelective(topic);
        return topic;
    }

    @Override
    public WxTopicListVo listWx(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria1 = topicExample.createCriteria();//sql语句的拼接${}
        criteria1.andDeletedEqualTo(false);//逻辑删除为false
        List<WxTopicListTopicVo> topicVos = topicMapper.selectWxTopicListTopicVoByExample(topicExample);
        PageInfo<WxTopicListTopicVo> topicVoPageInfo = new PageInfo<>(topicVos);
        long count = topicVoPageInfo.getTotal();

        WxTopicListVo topicListVo = new WxTopicListVo();
        topicListVo.setData(topicVos);
        topicListVo.setCount(count);
        return topicListVo;
    }

    @Override
    public WxTopicDetailVo detailWx(Integer id) {
        Topic topic = topicMapper.selectByPrimaryKey(id);
        WxTopicDetailVo wxTopicDetailVo = new WxTopicDetailVo();
        wxTopicDetailVo.setTopic(topic);
        wxTopicDetailVo.setGoods(topic.getGoods());
        return wxTopicDetailVo;
    }

    @Override
    public List<Topic> related(Integer id) {
        List<Topic> topicList = topicMapper.selectLimit();
        return topicList;
    }


}
